<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  

  
  <title>Redis快速入门 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Redis是一个开源，高级的键值存储和一个适用的解决方案，用于构建高性能，可扩展的web应用程序。 Redis有如下三个主要特点。  Redis将其数据库完全保存于内存中，仅适用磁盘进行持久化 与其他键值数据存储相比，Redis有一组相对丰富的数据类型 Redis可以将数据复制到任意数量的从机中。">
<meta name="keywords" content="redis">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis快速入门">
<meta property="og:url" content="http://yoursite.com/2018/10/12/2018-03-04-Redis快速入门/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="Redis是一个开源，高级的键值存储和一个适用的解决方案，用于构建高性能，可扩展的web应用程序。 Redis有如下三个主要特点。  Redis将其数据库完全保存于内存中，仅适用磁盘进行持久化 与其他键值数据存储相比，Redis有一组相对丰富的数据类型 Redis可以将数据复制到任意数量的从机中。">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-11-01T14:24:04.927Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Redis快速入门">
<meta name="twitter:description" content="Redis是一个开源，高级的键值存储和一个适用的解决方案，用于构建高性能，可扩展的web应用程序。 Redis有如下三个主要特点。  Redis将其数据库完全保存于内存中，仅适用磁盘进行持久化 与其他键值数据存储相比，Redis有一组相对丰富的数据类型 Redis可以将数据复制到任意数量的从机中。">
  
    <link rel="alternate" href="/org/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/org/css/style.css">
</head>
</html>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/org/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/org/">Home</a>
        
          <a class="main-nav-link" href="/org/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/org/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-2018-03-04-Redis快速入门" class="article article-type-post" itemscope="" itemprop="blogPost">
  <div class="article-meta">
    <a href="/org/2018/10/12/2018-03-04-Redis快速入门/" class="article-date">
  <time datetime="2018-10-12T09:03:30.000Z" itemprop="datePublished">2018-10-12</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Redis快速入门
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>Redis是一个开源，高级的键值存储和一个适用的解决方案，用于构建高性能，可扩展的web应用程序。</p>
<p>Redis有如下三个主要特点。</p>
<ul>
<li>Redis将其数据库完全保存于内存中，仅适用磁盘进行持久化</li>
<li>与其他键值数据存储相比，Redis有一组相对丰富的数据类型</li>
<li>Redis可以将数据复制到任意数量的从机中。</li>
</ul>
<a id="more"></a>
<p><a href="http://www.redis.io" target="_blank" rel="noopener">Redis的官网</a></p>
<p>以下是Redis的一些优点:</p>
<ul>
<li>Redis非常快，每秒可执行大约<code>110000</code>次的设置(<code>SET</code>)操作，每秒大约可执行<code>81000</code>次的读取/获取(<code>GET</code>)操作。</li>
<li><strong>支持丰富的数据类型</strong> - Redis支持开发人员常用的大多数数据类型，例如列表，集合，排序集和散列等等。这使得Redis很容易被用来解决各种问题，因为我们知道哪些问题可以更好使用地哪些数据类型来处理解决。</li>
<li><strong>操作具有原子性</strong> - 所有Redis操作都是原子操作，这确保如果两个客户端并发访问，Redis服务器能接收更新的值。</li>
<li><strong>多实用工具</strong> - Redis是一个多实用工具，可用于多种用例，如：缓存，消息队列(Redis本地支持发布/订阅)，应用程序中的任何短期数据，例如，web应用程序中的会话，网页命中计数等。</li>
</ul>
<h2 id="Redis与其他键值存储系统"><a href="#Redis与其他键值存储系统" class="headerlink" title="Redis与其他键值存储系统"></a>Redis与其他键值存储系统</h2><ul>
<li>Redis是键值数据库系统的不同进化路线，它的值可以包含更复杂的数据类型，可在这些数据类型上定义原子操作</li>
<li>Redis是一个内存数据库，但在磁盘数据库上是持久的,因此它代表了一个不同的权衡，在这种情况下，在不能大于存储器的数据集的限制下实现非常高的写和读速度</li>
<li>内存数据库的另一个优点是，它与磁盘上的相同数据结构相比，复杂数据结构在内存洪存储表示更容易操作。因此，Redis可以做很少的内部复杂性。</li>
</ul>
<h2 id="1-Redis环境安装配置"><a href="#1-Redis环境安装配置" class="headerlink" title="1. Redis环境安装配置"></a>1. Redis环境安装配置</h2><h2 id="在Ubuntu上安装Redis"><a href="#在Ubuntu上安装Redis" class="headerlink" title="在Ubuntu上安装Redis"></a>在Ubuntu上安装Redis</h2><p>要在Ubuntu上安装Redis，打开终端并键入以下命令 -</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[yiibai@ubuntu:~]$ sudo apt-get update </span><br><span class="line">[yiibai@ubuntu:~]$ sudo apt-get install redis-server</span><br></pre></td></tr></table></figure>
<p>这将在Ubuntu机器上安装Redis。</p>
<p><strong>启动Redis</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">[yiibai@ubuntu:~]$ redis-server</span><br><span class="line">[2988] 07 Feb 17:09:42.485 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf</span><br><span class="line">[2988] 07 Feb 17:09:42.488 # Unable to set the max number of files limit to 10032 (Operation not permitted), setting the max clients configuration to 3984.</span><br><span class="line">[2988] 07 Feb 17:09:42.490 # Warning: 32 bit instance detected but no memory lim</span><br><span class="line">                _._</span><br><span class="line">           _.-``__ &apos;&apos;-._</span><br><span class="line">      _.-``    `.  `_.  &apos;&apos;-._           Redis 2.8.4 (00000000/0) 32 bit</span><br><span class="line">  .-`` .-```.  ```\/    _.,_ &apos;&apos;-._</span><br><span class="line"> (    &apos;      ,       .-`  | `,    )     Running in stand alone mode</span><br><span class="line"> |`-._`-...-` __...-.``-._|&apos;` _.-&apos;|     Port: 6379</span><br><span class="line"> |    `-._   `._    /     _.-&apos;    |     PID: 2988</span><br><span class="line">  `-._    `-._  `-./  _.-&apos;    _.-&apos;</span><br><span class="line"> |`-._`-._    `-.__.-&apos;    _.-&apos;_.-&apos;|</span><br><span class="line"> |    `-._`-._        _.-&apos;_.-&apos;    |           http://redis.io</span><br><span class="line">  `-._    `-._`-.__.-&apos;_.-&apos;    _.-&apos;</span><br><span class="line"> |`-._`-._    `-.__.-&apos;    _.-&apos;_.-&apos;|</span><br><span class="line"> |    `-._`-._        _.-&apos;_.-&apos;    |</span><br><span class="line">  `-._    `-._`-.__.-&apos;_.-&apos;    _.-&apos;</span><br><span class="line">      `-._    `-.__.-&apos;    _.-&apos;</span><br><span class="line">          `-._        _.-&apos;</span><br><span class="line">              `-.__.-&apos;</span><br><span class="line"></span><br><span class="line">[2988] 07 Feb 17:09:42.581 # Server started, Redis version 2.8.4</span><br><span class="line">[2988] 07 Feb 17:09:42.582 # WARNING overcommit_memory is set to 0! Background s                                                                                        &apos; to /etc/sysctl.conf and then reboot or run the command &apos;sysctl vm.overcommit_m</span><br><span class="line">[2988] 07 Feb 17:09:42.582 * The server is now ready to accept connections on po</span><br></pre></td></tr></table></figure>
<p><strong>检查Redis是否正在工作</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[yiibai@ubuntu:~]$ redis-cli</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>这将打开一个<strong>redis</strong>提示，如下所示 -</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>在上面的提示中，<code>127.0.0.1</code>是计算机的IP地址，<code>6379</code>是运行<strong>Redis</strong>服务器的端口。 现在键入以下<code>PING</code>命令。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; ping </span><br><span class="line">PONG</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>这表明<strong>Redis</strong>已成功在您的计算机上安装了。</p>
<p>在Ubuntu上安装<strong>Redis桌面管理</strong></p>
<p>要在Ubuntu上安装<strong>Redis</strong>桌面管理器，可从 <a href="http://redisdesktop.com/download" target="_blank" rel="noopener">http://redisdesktop.com/download</a> 下载该软件包，安装即可。</p>
<p>打开下载的软件包并安装。</p>
<p><strong>Redis桌面管理器</strong>将提供用于管理Redis的键和数据的UI。</p>
<h2 id="2-Redis配置"><a href="#2-Redis配置" class="headerlink" title="2. Redis配置"></a>2. Redis配置</h2><p>在Redis中，在Redis的根目录下有一个配置文件(<code>redis.conf</code>)。当然您可以通过Redis <code>CONFIG</code>命令获取和设置所有的<strong>Redis</strong>配置。</p>
<p><strong>语法</strong><br>以下是Redis中的<code>CONFIG</code>命令的基本语法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; CONFIG GET CONFIG_SETTING_NAME</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p><strong>示例</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; CONFIG GET loglevel  </span><br><span class="line">1) &quot;loglevel&quot; </span><br><span class="line">2) &quot;notice&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<blockquote>
<p>要获取所有配置设置，请使用<code>*</code>代替<code>CONFIG_SETTING_NAME</code></p>
</blockquote>
<p><strong>示例</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; CONFIG GET *</span><br><span class="line">  1) &quot;dbfilename&quot;</span><br><span class="line">  2) &quot;dump.rdb&quot;</span><br><span class="line">  3) &quot;requirepass&quot;</span><br><span class="line">  4) &quot;&quot;</span><br><span class="line">  5) &quot;masterauth&quot;</span><br><span class="line">  6) &quot;&quot;</span><br><span class="line">  7) &quot;unixsocket&quot;</span><br><span class="line">  8) &quot;&quot;</span><br><span class="line">  9) &quot;logfile&quot;</span><br><span class="line"> 10) &quot;/var/log/redis/redis-server.log&quot;</span><br><span class="line"> 11) &quot;pidfile&quot;</span><br><span class="line"> 12) &quot;/var/run/redis/redis-server.pid&quot;</span><br><span class="line"> 13) &quot;maxmemory&quot;</span><br><span class="line"> 14) &quot;3221225472&quot;</span><br><span class="line"> 15) &quot;maxmemory-samples&quot;</span><br><span class="line"> 16) &quot;3&quot;</span><br><span class="line"> 17) &quot;timeout&quot;</span><br><span class="line"> 18) &quot;0&quot;</span><br><span class="line"> 19) &quot;tcp-keepalive&quot;</span><br><span class="line"> 20) &quot;0&quot;</span><br><span class="line"> 21) &quot;auto-aof-rewrite-percentage&quot;</span><br><span class="line"> 22) &quot;100&quot;</span><br><span class="line"> 23) &quot;auto-aof-rewrite-min-size&quot;</span><br><span class="line"> 24) &quot;67108864&quot;</span><br><span class="line"> 25) &quot;hash-max-ziplist-entries&quot;</span><br><span class="line"> 26) &quot;512&quot;</span><br><span class="line"> 27) &quot;hash-max-ziplist-value&quot;</span><br><span class="line"> 28) &quot;64&quot;</span><br><span class="line"> 29) &quot;list-max-ziplist-entries&quot;</span><br><span class="line"> 30) &quot;512&quot;</span><br><span class="line"> 31) &quot;list-max-ziplist-value&quot;</span><br><span class="line"> 32) &quot;64&quot;</span><br><span class="line"> 33) &quot;set-max-intset-entries&quot;</span><br><span class="line"> 34) &quot;512&quot;</span><br><span class="line"> 35) &quot;zset-max-ziplist-entries&quot;</span><br><span class="line"> 36) &quot;128&quot;</span><br><span class="line"> 37) &quot;zset-max-ziplist-value&quot;</span><br><span class="line"> 38) &quot;64&quot;</span><br><span class="line"> 39) &quot;lua-time-limit&quot;</span><br><span class="line"> 40) &quot;5000&quot;</span><br><span class="line"> 41) &quot;slowlog-log-slower-than&quot;</span><br><span class="line"> 42) &quot;10000&quot;</span><br><span class="line"> 43) &quot;slowlog-max-len&quot;</span><br><span class="line"> 44) &quot;128&quot;</span><br><span class="line"> 45) &quot;port&quot;</span><br><span class="line"> 46) &quot;6379&quot;</span><br><span class="line"> 47) &quot;databases&quot;</span><br><span class="line"> 48) &quot;16&quot;</span><br><span class="line"> 49) &quot;repl-ping-slave-period&quot;</span><br><span class="line"> 50) &quot;10&quot;</span><br><span class="line"> 51) &quot;repl-timeout&quot;</span><br><span class="line"> 52) &quot;60&quot;</span><br><span class="line"> 53) &quot;repl-backlog-size&quot;</span><br><span class="line"> 54) &quot;1048576&quot;</span><br><span class="line"> 55) &quot;repl-backlog-ttl&quot;</span><br><span class="line"> 56) &quot;3600&quot;</span><br><span class="line"> 57) &quot;maxclients&quot;</span><br><span class="line"> 58) &quot;3984&quot;</span><br><span class="line"> 59) &quot;watchdog-period&quot;</span><br><span class="line"> 60) &quot;0&quot;</span><br><span class="line"> 61) &quot;slave-priority&quot;</span><br><span class="line"> 62) &quot;100&quot;</span><br><span class="line"> 63) &quot;min-slaves-to-write&quot;</span><br><span class="line"> 64) &quot;0&quot;</span><br><span class="line"> 65) &quot;min-slaves-max-lag&quot;</span><br><span class="line"> 66) &quot;10&quot;</span><br><span class="line"> 67) &quot;hz&quot;</span><br><span class="line"> 68) &quot;10&quot;</span><br><span class="line"> 69) &quot;no-appendfsync-on-rewrite&quot;</span><br><span class="line"> 70) &quot;no&quot;</span><br><span class="line"> 71) &quot;slave-serve-stale-data&quot;</span><br><span class="line"> 72) &quot;yes&quot;</span><br><span class="line"> 73) &quot;slave-read-only&quot;</span><br><span class="line"> 74) &quot;yes&quot;</span><br><span class="line"> 75) &quot;stop-writes-on-bgsave-error&quot;</span><br><span class="line"> 76) &quot;yes&quot;</span><br><span class="line"> 77) &quot;daemonize&quot;</span><br><span class="line"> 78) &quot;yes&quot;</span><br><span class="line"> 79) &quot;rdbcompression&quot;</span><br><span class="line"> 80) &quot;yes&quot;</span><br><span class="line"> 81) &quot;rdbchecksum&quot;</span><br><span class="line"> 82) &quot;yes&quot;</span><br><span class="line"> 83) &quot;activerehashing&quot;</span><br><span class="line"> 84) &quot;yes&quot;</span><br><span class="line"> 85) &quot;repl-disable-tcp-nodelay&quot;</span><br><span class="line"> 86) &quot;no&quot;</span><br><span class="line"> 87) &quot;aof-rewrite-incremental-fsync&quot;</span><br><span class="line"> 88) &quot;yes&quot;</span><br><span class="line"> 89) &quot;appendonly&quot;</span><br><span class="line"> 90) &quot;no&quot;</span><br><span class="line"> 91) &quot;dir&quot;</span><br><span class="line"> 92) &quot;/var/lib/redis&quot;</span><br><span class="line"> 93) &quot;maxmemory-policy&quot;</span><br><span class="line"> 94) &quot;noeviction&quot;</span><br><span class="line"> 95) &quot;appendfsync&quot;</span><br><span class="line"> 96) &quot;everysec&quot;</span><br><span class="line"> 97) &quot;save&quot;</span><br><span class="line"> 98) &quot;900 1 300 10 60 10000&quot;</span><br><span class="line"> 99) &quot;loglevel&quot;</span><br><span class="line">100) &quot;notice&quot;</span><br><span class="line">101) &quot;client-output-buffer-limit&quot;</span><br><span class="line">102) &quot;normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60&quot;</span><br><span class="line">103) &quot;unixsocketperm&quot;</span><br><span class="line">104) &quot;0&quot;</span><br><span class="line">105) &quot;slaveof&quot;</span><br><span class="line">106) &quot;&quot;</span><br><span class="line">107) &quot;notify-keyspace-events&quot;</span><br><span class="line">108) &quot;&quot;</span><br><span class="line">109) &quot;bind&quot;</span><br><span class="line">110) &quot;127.0.0.1&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<h2 id="编辑配置"><a href="#编辑配置" class="headerlink" title="编辑配置"></a>编辑配置</h2><p>要更新配置，可以直接编辑<code>redis.conf</code>文件，也可以通过<code>CONFIG set</code>命令更新配置。</p>
<p><strong>语法</strong><br>以下是<code>CONFIG SET</code>命令的基本语法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p><strong>示例</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; CONFIG SET loglevel &quot;notice&quot; </span><br><span class="line">OK </span><br><span class="line">redis 127.0.0.1:6379&gt; CONFIG GET loglevel  </span><br><span class="line">1) &quot;loglevel&quot; </span><br><span class="line">2) &quot;notice&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<h2 id="3-Redis数据类型"><a href="#3-Redis数据类型" class="headerlink" title="3. Redis数据类型"></a>3. Redis数据类型</h2><p>Redis支持<code>5</code>种数据类型。</p>
<h2 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h2><p>Redis中的字符串是一个字节序列。Redis中的字符串是二进制安全的，这意味着它们的长度不由任何特殊的终止字符决定。因此，可以在一个字符串中存储高达<code>512</code>兆字节的任何内容。</p>
<p><strong>示例</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; set name &quot;yiibai.com&quot; </span><br><span class="line">OK </span><br><span class="line">redis 127.0.0.1:6379&gt; get name </span><br><span class="line">&quot;yiibai.com&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>在上面的示例中，<code>set</code>和<code>get</code>是Redis命令，<code>name</code>是Redis中使用的键，<code>yiibai.com</code>是存储在Redis中的字符串的值。</p>
<blockquote>
<p>注 - Redis命令不区分大小写，如<code>SET</code>,<code>Set</code>和<code>set</code>都是同一个命令。字符串值的最大长度为 512MB。</p>
</blockquote>
<h2 id="散列-哈希"><a href="#散列-哈希" class="headerlink" title="散列/哈希"></a>散列/哈希</h2><p>Redis散列/哈希(Hashes)是键值对的集合。Redis散列/哈希是字符串字段和字符串值之间的映射。因此，它们用于表示对象。</p>
<p><strong>示例</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; HMSET ukey username &quot;yiibai&quot; password &quot;passswd123&quot; points 200</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>在上述示例中，散列/哈希数据类型用于存储包含用户的基本信息的用户对象。这里<code>HMSET</code>，<code>HGETALL</code>是Redis的命令，而<code>ukey</code>是键的名称。</p>
<p>每个散列/哈希可以存储多达<code>2^32 - 1</code>个健-值对(超过<code>40</code>亿个)。</p>
<h2 id="列表"><a href="#列表" class="headerlink" title="列表"></a>列表</h2><p>Redis列表只是字符串列表，按插入顺序排序。您可以向Redis列表的头部或尾部添加元素。</p>
<p><strong>示例</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; lpush alist redis </span><br><span class="line">(integer) 1 </span><br><span class="line">redis 127.0.0.1:6379&gt; lpush alist mongodb </span><br><span class="line">(integer) 2 </span><br><span class="line">redis 127.0.0.1:6379&gt; lpush alist sqlite </span><br><span class="line">(integer) 3 </span><br><span class="line">redis 127.0.0.1:6379&gt; lrange alist 0 10  </span><br><span class="line"></span><br><span class="line">1) &quot;sqlite&quot; </span><br><span class="line">2) &quot;mongodb&quot; </span><br><span class="line">3) &quot;redis&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>列表的最大长度为<code>2^32 - 1</code>个元素(<code>4294967295</code>，每个列表可容纳超过<code>40</code>亿个元素)。</p>
<h2 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h2><p>Redis集合是字符串的无序集合。在Redis中，您可以添加，删除和测试成员存在的时间O(1)复杂性。</p>
<p><strong>示例</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; sadd yiibailist redis </span><br><span class="line">(integer) 1 </span><br><span class="line">redis 127.0.0.1:6379&gt; sadd yiibailist mongodb </span><br><span class="line">(integer) 1 </span><br><span class="line">redis 127.0.0.1:6379&gt; sadd yiibailist sqlite </span><br><span class="line">(integer) 1 </span><br><span class="line">redis 127.0.0.1:6379&gt; sadd yiibailist sqlite </span><br><span class="line">(integer) 0 </span><br><span class="line">redis 127.0.0.1:6379&gt; smembers yiibailist  </span><br><span class="line"></span><br><span class="line">1) &quot;sqlite&quot; </span><br><span class="line">2) &quot;mongodb&quot; </span><br><span class="line">3) &quot;redis&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意 - 在上面的示例中，<code>sqlite</code>被添加了两次，但是由于集合的唯一属性，所以它只算添加一次。</p>
</blockquote>
<p>一个集合中的最大成员数量为<code>2^32 - 1</code>(即<code>4294967295</code>，每个集合中元素数量可达<code>40</code>亿个)个。</p>
<h2 id="可排序集合"><a href="#可排序集合" class="headerlink" title="可排序集合"></a>可排序集合</h2><p>Redis可排序集合类似于Redis集合，是不重复的字符集合。 不同之处在于，排序集合的每个成员都与分数相关联，这个分数用于按最小分数到最大分数来排序的排序集合。虽然成员是唯一的，但分数值可以重复。</p>
<p><strong>示例</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; zadd yiibaiset 0 redis</span><br><span class="line">(integer) 1 </span><br><span class="line">redis 127.0.0.1:6379&gt; zadd yiibaiset 0 mongodb</span><br><span class="line">(integer) 1 </span><br><span class="line">redis 127.0.0.1:6379&gt; zadd yiibaiset 1 sqlite</span><br><span class="line">(integer) 1 </span><br><span class="line">redis 127.0.0.1:6379&gt; zadd yiibaiset 1 sqlite</span><br><span class="line">(integer) 0 </span><br><span class="line">redis 127.0.0.1:6379&gt; ZRANGEBYSCORE yiibaiset 0 1000  </span><br><span class="line"></span><br><span class="line">1) &quot;mongodb&quot; </span><br><span class="line">2) &quot;redis&quot; </span><br><span class="line">3) &quot;sqlite&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>因为 ‘<code>sqlite</code>‘ 的排序值是 1 ，其它两个元素的排序值是 0 ，所以 ‘<code>sqlite</code>‘ 排在最后一个位置上。</p>
<h2 id="4-Redis命令"><a href="#4-Redis命令" class="headerlink" title="4. Redis命令"></a>4. Redis命令</h2><p>Redis命令是用于在Redis服务器上执行一些操作。<br>要在Redis服务器上运行命令，需要一个Redis客户端。Redis客户端在Redis包中有提供，这个包在我们前面的安装教程中就有安装过了。</p>
<p><strong>语法</strong><br>以下是Redis客户端的基本语法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[yiibai@ubuntu:~]$ redis-cli</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p><strong>示例</strong><br>以下示例说明了如何启动Redis客户端。</p>
<p>要启动Redis客户端，请打开终端并键入命令<code>redis-cli</code>。 这将连接到您的本地Redis服务器，现在可以运行任何的Redis命令了。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[yiibai@ubuntu:~]$redis-cli </span><br><span class="line">redis 127.0.0.1:6379&gt; </span><br><span class="line">redis 127.0.0.1:6379&gt; PING  </span><br><span class="line">PONG</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>在上面的示例中，连接到到在本地机器上运行的Redis服务器并执行<code>PING</code>命令，该命令检查服务器是否正在运行。</p>
<h2 id="在远程服务器上运行命令"><a href="#在远程服务器上运行命令" class="headerlink" title="在远程服务器上运行命令"></a>在远程服务器上运行命令</h2><p>要在Redis远程服务器上运行命令，需要通过客户端<code>redis-cli</code>连接到服务器</p>
<p><strong>语法</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[yiibai@ubuntu:~]$ redis-cli -h host -p port -a password</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p><strong>示例</strong><br>以下示例显示如何连接到Redis远程服务器，在主机(host)<code>127.0.0.1</code>，端口(port)<code>6379</code>上运行，并使用密码为 <code>mypass</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[yiibai@ubuntu:~]$ redis-cli -h 127.0.0.1 -p 6379 -a &quot;mypass&quot; </span><br><span class="line">redis 127.0.0.1:6379&gt; </span><br><span class="line">redis 127.0.0.1:6379&gt; PING  </span><br><span class="line">PONG</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<h2 id="5-Redis键命令"><a href="#5-Redis键命令" class="headerlink" title="5. Redis键命令"></a>5. Redis键命令</h2><p>Redis键命令用于管理<strong>Redis</strong>中的键。以下是使用redis键命令的语法。</p>
<p><strong>语法</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; COMMAND KEY_NAME</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p><strong>示例</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; SET akey redis</span><br><span class="line">OK </span><br><span class="line">redis 127.0.0.1:6379&gt; DEL akey</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; GET akey</span><br><span class="line">(nil)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>在上面的例子中，<code>DEL</code>是Redis的命令，而<code>akey</code>是键的名称。如果键被删除，则命令的输出将为<code>(integer) 1</code>，否则为<code>(integer) 0</code>。</p>
<h2 id="Redis键命令"><a href="#Redis键命令" class="headerlink" title="Redis键命令"></a>Redis键命令</h2><p>下表列出了与键相关的一些基本命令。</p>
<table>
<thead>
<tr>
<th>编号</th>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a href="http://www.yiibai.com/redis/keys_del.html" target="_blank" rel="noopener">DEL key</a></td>
<td>此命令删除一个指定键(如果存在)。</td>
</tr>
<tr>
<td>2</td>
<td><a href="http://www.yiibai.com/redis/keys_dump.html" target="_blank" rel="noopener">DUMP key</a></td>
<td>此命令返回存储在指定键的值的序列化版本。</td>
</tr>
<tr>
<td>3</td>
<td><a href="http://www.yiibai.com/redis/keys_exists.html" target="_blank" rel="noopener">EXISTS key</a></td>
<td>此命令检查键是否存在。</td>
</tr>
<tr>
<td>4</td>
<td><a href="http://www.yiibai.com/redis/keys_expire.html" target="_blank" rel="noopener">EXPIRE key seconds</a></td>
<td>设置键在指定时间秒数之后到期/过期。</td>
</tr>
<tr>
<td>5</td>
<td><a href="http://www.yiibai.com/redis/keys_expireat.html" target="_blank" rel="noopener">EXPIREAT key timestamp</a></td>
<td>设置在指定时间戳之后键到期/过期。这里的时间是Unix时间戳格式。</td>
</tr>
<tr>
<td>6</td>
<td><a href="http://www.yiibai.com/redis/keys_pexpire.html" target="_blank" rel="noopener">PEXPIRE key milliseconds</a></td>
<td>设置键的到期时间(以毫秒为单位)。</td>
</tr>
<tr>
<td>7</td>
<td><a href="http://www.yiibai.com/redis/keys_pexpireat.html" target="_blank" rel="noopener">PEXPIREAT key milliseconds-timestamp</a></td>
<td>以Unix时间戳形式来设置键的到期时间(以毫秒为单位)。</td>
</tr>
<tr>
<td>8</td>
<td><a href="http://www.yiibai.com/redis/keys_keys.html" target="_blank" rel="noopener">KEYS pattern</a></td>
<td>查找与指定模式匹配的所有键。</td>
</tr>
<tr>
<td>9</td>
<td><a href="http://www.yiibai.com/redis/keys_move.html" target="_blank" rel="noopener">MOVE key db</a></td>
<td>将键移动到另一个数据库。</td>
</tr>
<tr>
<td>10</td>
<td><a href="http://www.yiibai.com/redis/keys_persist.html" target="_blank" rel="noopener">PERSIST key</a></td>
<td>删除指定键的过期时间，得永生。</td>
</tr>
<tr>
<td>11</td>
<td><a href="http://www.yiibai.com/redis/keys_pttl.html" target="_blank" rel="noopener">PTTL key</a></td>
<td>获取键的剩余到期时间。</td>
</tr>
<tr>
<td>12</td>
<td><a href="http://www.yiibai.com/redis/keys_randomkey.html" target="_blank" rel="noopener">RANDOMKEY</a></td>
<td>从Redis返回一个随机的键。</td>
</tr>
<tr>
<td>13</td>
<td><a href="http://www.yiibai.com/redis/keys_rename.html" target="_blank" rel="noopener">RENAME key newkey</a></td>
<td>更改键的名称。</td>
</tr>
<tr>
<td>14</td>
<td><a href="http://www.yiibai.com/redis/keys_pttl.html" target="_blank" rel="noopener">PTTL key</a></td>
<td>获取键到期的剩余时间(以毫秒为单位)。</td>
</tr>
<tr>
<td>15</td>
<td><a href="http://www.yiibai.com/redis/keys_renamenx.html" target="_blank" rel="noopener">RENAMENX key newkey</a></td>
<td>如果新键不存在，重命名键。</td>
</tr>
<tr>
<td>16</td>
<td><a href="http://www.yiibai.com/redis/keys_type.html" target="_blank" rel="noopener">TYPE key</a></td>
<td>返回存储在键中的值的数据类型。</td>
</tr>
</tbody>
</table>
<h2 id="6-Redis字符串"><a href="#6-Redis字符串" class="headerlink" title="6. Redis字符串"></a>6. Redis字符串</h2><p>Redis字符串命令用于管理Redis中的字符串值。以下是使用Redis字符串命令的语法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; COMMAND KEY_NAME</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p><strong>示例</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; SET mykey &quot;redis&quot; </span><br><span class="line">OK </span><br><span class="line">redis 127.0.0.1:6379&gt; GET mykey </span><br><span class="line">&quot;redis&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>在上面的例子中，<code>SET</code>和<code>GET</code>是redis中的命令，而<code>mykey</code>是键的名称。</p>
<h2 id="Redis字符串命令"><a href="#Redis字符串命令" class="headerlink" title="Redis字符串命令"></a>Redis字符串命令</h2><p>下表列出了一些用于在<strong>Redis</strong>中管理字符串的基本命令。</p>
<table>
<thead>
<tr>
<th>编号</th>
<th>命令</th>
<th>描述说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a href="http://www.yiibai.com/redis/strings_set.html" target="_blank" rel="noopener">SET key value</a></td>
<td>此命令设置指定键的值。</td>
</tr>
<tr>
<td>2</td>
<td><a href="http://www.yiibai.com/redis/strings_get.html" target="_blank" rel="noopener">GET key</a></td>
<td>获取指定键的值。</td>
</tr>
<tr>
<td>3</td>
<td><a href="http://www.yiibai.com/redis/strings_getrange.html" target="_blank" rel="noopener">GETRANGE key start end</a></td>
<td>获取存储在键上的字符串的子字符串。</td>
</tr>
<tr>
<td>4</td>
<td><a href="http://www.yiibai.com/redis/strings_getset.html" target="_blank" rel="noopener">GETSET key value</a></td>
<td>设置键的字符串值并返回其旧值。</td>
</tr>
<tr>
<td>5</td>
<td><a href="http://www.yiibai.com/redis/strings_getbit.html" target="_blank" rel="noopener">GETBIT key offset</a></td>
<td>返回在键处存储的字符串值中偏移处的位值。</td>
</tr>
<tr>
<td>6</td>
<td><a href="http://www.yiibai.com/redis/strings_mget.html" target="_blank" rel="noopener">MGET key1 [key2..]</a></td>
<td>获取所有给定键的值</td>
</tr>
<tr>
<td>7</td>
<td><a href="http://www.yiibai.com/redis/strings_setbit.html" target="_blank" rel="noopener">SETBIT key offset value</a></td>
<td>存储在键上的字符串值中设置或清除偏移处的位</td>
</tr>
<tr>
<td>8</td>
<td><a href="http://www.yiibai.com/redis/strings_setex.html" target="_blank" rel="noopener">SETEX key seconds value</a></td>
<td>使用键和到期时间来设置值</td>
</tr>
<tr>
<td>9</td>
<td><a href="http://www.yiibai.com/redis/strings_setnx.html" target="_blank" rel="noopener">SETNX key value</a></td>
<td>设置键的值，仅当键不存在时</td>
</tr>
<tr>
<td>10</td>
<td><a href="http://www.yiibai.com/redis/strings_setrange.html" target="_blank" rel="noopener">SETRANGE key offset value</a></td>
<td>在指定偏移处开始的键处覆盖字符串的一部分</td>
</tr>
<tr>
<td>11</td>
<td><a href="http://www.yiibai.com/redis/strings_strlen.html" target="_blank" rel="noopener">STRLEN key</a></td>
<td>获取存储在键中的值的长度</td>
</tr>
<tr>
<td>12</td>
<td><a href="http://www.yiibai.com/redis/strings_mset.html" target="_blank" rel="noopener">MSET key value [key value …]</a></td>
<td>为多个键分别设置它们的值</td>
</tr>
<tr>
<td>13</td>
<td><a href="http://www.yiibai.com/redis/strings_msetnx.html" target="_blank" rel="noopener">MSETNX key value [key value …]</a></td>
<td>为多个键分别设置它们的值，仅当键不存在时</td>
</tr>
<tr>
<td>14</td>
<td><a href="http://www.yiibai.com/redis/strings_psetex.html" target="_blank" rel="noopener">PSETEX key milliseconds value</a></td>
<td>设置键的值和到期时间(以毫秒为单位)</td>
</tr>
<tr>
<td>15</td>
<td><a href="http://www.yiibai.com/redis/strings_incr.html" target="_blank" rel="noopener">INCR key</a></td>
<td>将键的整数值增加<code>1</code></td>
</tr>
<tr>
<td>16</td>
<td><a href="http://www.yiibai.com/redis/strings_incrby.html" target="_blank" rel="noopener">INCRBY key increment</a></td>
<td>将键的整数值按给定的数值增加</td>
</tr>
<tr>
<td>17</td>
<td><a href="http://www.yiibai.com/redis/strings_incrbyfloat.html" target="_blank" rel="noopener">INCRBYFLOAT key increment</a></td>
<td>将键的浮点值按给定的数值增加</td>
</tr>
<tr>
<td>18</td>
<td><a href="http://www.yiibai.com/redis/strings_decr.html" target="_blank" rel="noopener">DECR key</a></td>
<td>将键的整数值减<code>1</code></td>
</tr>
<tr>
<td>19</td>
<td><a href="http://www.yiibai.com/redis/strings_decrby.html" target="_blank" rel="noopener">DECRBY key decrement</a></td>
<td>按给定数值减少键的整数值</td>
</tr>
<tr>
<td>20</td>
<td><a href="http://www.yiibai.com/redis/strings_append.html" target="_blank" rel="noopener">APPEND key value</a></td>
<td>将指定值附加到键</td>
</tr>
</tbody>
</table>
<h2 id="7-Redis哈希"><a href="#7-Redis哈希" class="headerlink" title="7. Redis哈希"></a>7. Redis哈希</h2><p>Redis Hashes是字符串字段和字符串值之间的映射(类似于PHP中的数组类型)。 因此，它们是表示对象的完美数据类型。</p>
<p>在Redis中，每个哈希(散列)可以存储多达4亿个键-值对。</p>
<h2 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; HMSET myhash name &quot;redis tutorial&quot; </span><br><span class="line">description &quot;redis basic commands for caching&quot; likes 20 visitors 23000 </span><br><span class="line">OK </span><br><span class="line">127.0.0.1:6379&gt; HGETALL myhash</span><br><span class="line">1) &quot;field1&quot;</span><br><span class="line">2) &quot;Hello&quot;</span><br><span class="line">3) &quot;field2&quot;</span><br><span class="line">4) &quot;World&quot;</span><br><span class="line">5) &quot;name&quot;</span><br><span class="line">6) &quot;redis tutorial&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>在上面的例子中，在名称为’<code>myhash</code>‘的哈希中设置了Redis教程的详细信息(名称，描述，喜欢，访问者)。</p>
<h2 id="8-Redis列表"><a href="#8-Redis列表" class="headerlink" title="8. Redis列表"></a>8. Redis列表</h2><p>Redis列表只是字符串列表，按插入顺序排序。可以在列表的头部或尾部添加Redis列表中的元素。</p>
<p>列表的最大长度为<code>2^32 - 1</code>个元素(即<code>4294967295</code>，每个列表可存储超过40亿个元素)。</p>
<h2 id="示例-1"><a href="#示例-1" class="headerlink" title="示例"></a>示例</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; LPUSH mylist &quot;redis&quot; </span><br><span class="line">(integer) 1 </span><br><span class="line">redis 127.0.0.1:6379&gt; LPUSH mylist &quot;mongodb&quot;</span><br><span class="line">(integer) 2 </span><br><span class="line">redis 127.0.0.1:6379&gt; LPUSH mylist &quot;mysql&quot;</span><br><span class="line">(integer) 3 </span><br><span class="line">redis 127.0.0.1:6379&gt; LRANGE mylist 0 10  </span><br><span class="line">1) &quot;mysql&quot; </span><br><span class="line">2) &quot;mongodb&quot; </span><br><span class="line">3) &quot;redis&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>在上面的示例中，通过命令<code>LPUSH</code>将三个值插入到名称为“<code>mylist</code>”的Redis列表中。</p>
<h2 id="8-Redis集合"><a href="#8-Redis集合" class="headerlink" title="8. Redis集合"></a>8. Redis集合</h2><p>Redis集合是唯一字符串的无序集合。 唯一值表示集合中不允许键中有重复的数据。</p>
<p>在Redis中设置添加，删除和测试成员的存在(恒定时间O(1)，而不考虑集合中包含的元素数量)。列表的最大长度为<code>2^32 - 1</code>个元素(即4294967295，每组集合超过40亿个元素)。</p>
<h2 id="示例-2"><a href="#示例-2" class="headerlink" title="示例"></a>示例</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; SADD myset &quot;redis&quot; </span><br><span class="line">(integer) 1 </span><br><span class="line">redis 127.0.0.1:6379&gt; SADD myset &quot;mongodb&quot; </span><br><span class="line">(integer) 1 </span><br><span class="line">redis 127.0.0.1:6379&gt; SADD myset &quot;mysql&quot; </span><br><span class="line">(integer) 1 </span><br><span class="line">redis 127.0.0.1:6379&gt; SADD myset &quot;mysql&quot; </span><br><span class="line">(integer) 0 </span><br><span class="line">redis 127.0.0.1:6379&gt; SMEMBERS &quot;myset&quot;  </span><br><span class="line">1) &quot;mysql&quot; </span><br><span class="line">2) &quot;mongodb&quot; </span><br><span class="line">3) &quot;redis&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>在上面的示例中，通过命令<code>SADD</code>将三个值插入到名称为“<code>myset</code>”的Redis集合中。</p>
<h2 id="9-Redis发送订阅"><a href="#9-Redis发送订阅" class="headerlink" title="9. Redis发送订阅"></a>9. Redis发送订阅</h2><p>Redis发布订阅(pub/sub)是一种消息通信模式：发送者(pub)发送消息，订阅者(sub)接收消息。<br>Redis 发布订阅(pub/sub)实现了消息系统，发送者(在redis术语中称为发布者)在接收者(订阅者)接收消息时发送消息。传送消息的链路称为信道。</p>
<p>在Redis中，客户端可以订阅任意数量的信道。</p>
<h2 id="示例-3"><a href="#示例-3" class="headerlink" title="示例"></a>示例</h2><p>以下示例说明了发布用户概念的工作原理。 在以下示例中，一个客户端订阅名为“<code>redisChat</code>”的信道。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; SUBSCRIBE redisChat  </span><br><span class="line">Reading messages... (press Ctrl-C to quit) </span><br><span class="line">1) &quot;subscribe&quot; </span><br><span class="line">2) &quot;redisChat&quot; </span><br><span class="line">3) (integer) 1</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<p>现在，两个客户端在名称为“<code>redisChat</code>”的相同信道上发布消息，并且上述订阅的客户端接收消息。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; PUBLISH redisChat &quot;Redis is a great caching technique&quot;  </span><br><span class="line">(integer) 1  </span><br><span class="line">redis 127.0.0.1:6379&gt; PUBLISH redisChat &quot;Learn redis by yiibai&quot;  </span><br><span class="line">(integer) 1   </span><br><span class="line">1) &quot;message&quot; </span><br><span class="line">2) &quot;redisChat&quot; </span><br><span class="line">3) &quot;Redis is a great caching technique&quot; </span><br><span class="line">1) &quot;message&quot; </span><br><span class="line">2) &quot;redisChat&quot; </span><br><span class="line">3) &quot;Learn redis by yiibai&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<h2 id="10-Redis事务"><a href="#10-Redis事务" class="headerlink" title="10. Redis事务"></a>10. Redis事务</h2><p>Redis事务允许在单个步骤中执行一组命令。以下是事务的两个属性：</p>
<ul>
<li>事务中的所有命令作为单个隔离操作并按顺序执行。不可以在执行Redis事务的中间向另一个客户端发出的请求。</li>
<li>Redis事务也是原子的。原子意味着要么处理所有命令，要么都不处理。</li>
</ul>
<h2 id="语法示例"><a href="#语法示例" class="headerlink" title="语法示例"></a>语法示例</h2><p>Redis事务由命令<code>MULTI</code>命令启动，然后需要传递一个应该在事务中执行的命令列表，然后整个事务由<code>EXEC</code>命令执行。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; MULTI </span><br><span class="line">OK </span><br><span class="line">List of commands here </span><br><span class="line">redis 127.0.0.1:6379&gt; EXEC</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<h2 id="示例-4"><a href="#示例-4" class="headerlink" title="示例"></a>示例</h2><p>以下示例说明了如何启动和执行Redis事务。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; MULTI </span><br><span class="line">OK </span><br><span class="line">redis 127.0.0.1:6379&gt; SET mykey &quot;redis&quot; </span><br><span class="line">QUEUED </span><br><span class="line">redis 127.0.0.1:6379&gt; GET mykey </span><br><span class="line">QUEUED </span><br><span class="line">redis 127.0.0.1:6379&gt; INCR visitors </span><br><span class="line">QUEUED </span><br><span class="line">redis 127.0.0.1:6379&gt; EXEC  </span><br><span class="line">1) OK </span><br><span class="line">2) &quot;redis&quot; </span><br><span class="line">3) (integer) 1</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Shell</span><br></pre></td></tr></table></figure>
<h2 id="11-Redis脚本"><a href="#11-Redis脚本" class="headerlink" title="11. Redis脚本"></a>11. Redis脚本</h2>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/10/12/2018-03-04-Redis快速入门/" data-id="cjoztxu2b000f1wij527b4e08" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/org/tags/redis/">redis</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/org/2018/10/16/spring-cloud中的配置属性说明汇总/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          spring cloud中的配置属性说明汇总
        
      </div>
    </a>
  
  
    <a href="/org/2018/10/12/2018-03-01-shiro JSP标签/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Shiro JSP标签</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/SVN/">SVN</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/Spring-secrity/">Spring-secrity</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/hexo/">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/hibenrate/">hibenrate</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/jekyll/">jekyll</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/jenkins/">jenkins</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/redis/">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/shiro/">shiro</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spingMVC/">spingMVC</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spring-cloud/">spring cloud</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spring-cloud/">spring-cloud</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spring-secrity/">spring-secrity</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/springMVC/">springMVC</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/springboot/">springboot</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/生活/">生活</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/org/tags/SVN/" style="font-size: 12px;">SVN</a> <a href="/org/tags/Spring-secrity/" style="font-size: 10px;">Spring-secrity</a> <a href="/org/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/org/tags/hibenrate/" style="font-size: 10px;">hibenrate</a> <a href="/org/tags/jekyll/" style="font-size: 10px;">jekyll</a> <a href="/org/tags/jenkins/" style="font-size: 10px;">jenkins</a> <a href="/org/tags/redis/" style="font-size: 16px;">redis</a> <a href="/org/tags/shiro/" style="font-size: 18px;">shiro</a> <a href="/org/tags/spingMVC/" style="font-size: 10px;">spingMVC</a> <a href="/org/tags/spring-cloud/" style="font-size: 10px;">spring cloud</a> <a href="/org/tags/spring-cloud/" style="font-size: 10px;">spring-cloud</a> <a href="/org/tags/spring-secrity/" style="font-size: 12px;">spring-secrity</a> <a href="/org/tags/springMVC/" style="font-size: 14px;">springMVC</a> <a href="/org/tags/springboot/" style="font-size: 20px;">springboot</a> <a href="/org/tags/生活/" style="font-size: 10px;">生活</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/org/archives/2018/11/">November 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/org/archives/2018/10/">October 2018</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/org/2018/11/27/redis07-zookeeper-kafka集群部署以及如何使用简单介绍/">redis07-zookeeper+kafka集群部署以及如何使用简单介绍</a>
          </li>
        
          <li>
            <a href="/org/2018/11/13/redis06-cluster实现高可用性/">redis06-cluster实现高可用性</a>
          </li>
        
          <li>
            <a href="/org/2018/11/12/redis05-在项目中搭建读写分-高可用-多master的redis-cluster集群/">redis05-在项目中搭建读写分+高可用+多master的redis cluster集群</a>
          </li>
        
          <li>
            <a href="/org/2018/11/05/在项目中用经典的三节点方式部署哨兵集群-笔记/">在项目中用经典的三节点方式部署哨兵集群-笔记</a>
          </li>
        
          <li>
            <a href="/org/2018/11/05/redis哨兵的多个核心底层原理-笔记/">redis哨兵的多个核心底层原理-笔记</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2018 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/org/" class="mobile-nav-link">Home</a>
  
    <a href="/org/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//code.jquery.com/jquery-2.0.3.min.js"></script>


  <link rel="stylesheet" href="/org/fancybox/jquery.fancybox.css">
  <script src="/org/fancybox/jquery.fancybox.pack.js"></script>


<script src="/org/js/script.js"></script>



  </div>
</body>
</html>